﻿namespace OleDbDAL.Procurement
{
    using DBUtility;
    using IDal.Procurement;
    using Model.Procurement;
    using System;
    using System.Data;
    using System.Text;

    public class ProcurementPaymentApp : IProcurementPayment
    {
        private string dbname = "Systems";

        public void Add(ProcurementPaymentEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_ProcurementPayment(");
            builder.Append("ChainNum,PuDocuNum,SupplierId,PurType,AccountId,PayTime,CreateMan,CreateTime,AllMoney,IsInvoicing,InvoicingNum,CheckMan,CheckTime,IsCheck,IsClear,Remark");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.ChainNum + ",");
            builder.Append("'" + model.PuDocuNum + "',");
            builder.Append("" + model.SupplierId + ",");
            builder.Append("" + model.PurType + ",");
            builder.Append("" + model.AccountId + ",");
            builder.Append("'" + model.PayTime + "',");
            builder.Append("" + model.CreateMan + ",");
            builder.Append("'" + model.CreateTime + "',");
            builder.Append("" + model.AllMoney + ",");
            builder.Append("" + model.IsInvoicing + ",");
            builder.Append("'" + model.InvoicingNum + "',");
            builder.Append("" + model.CheckMan + ",");
            builder.Append("'" + model.CheckTime + "',");
            builder.Append("" + model.IsCheck + ",");
            builder.Append("" + model.IsClear + ",");
            builder.Append("'" + model.Remark + "'");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int PurchaseId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_ProcurementPayment ");
            builder.Append(" where PurchaseId=" + PurchaseId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_ProcurementPayment ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by PurchaseId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "PurchaseId", "T_ProcurementPayment", swhere);
        }

        public ProcurementPaymentEnt GetModel(int PurchaseId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select PurchaseId,ChainNum,PuDocuNum,SupplierId,PurType,AccountId,PayTime,CreateMan,CreateTime,AllMoney,IsInvoicing,InvoicingNum,CheckMan,CheckTime,IsCheck,IsClear,IsBlackOut,BlackManId,BlackManName,BlackTime,Remark from T_ProcurementPayment ");
            builder.Append(" where PurchaseId=" + PurchaseId);
            ProcurementPaymentEnt ent = new ProcurementPaymentEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.PurchaseId = PurchaseId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                ent.PuDocuNum = table.Rows[0]["PuDocuNum"].ToString();
                if (table.Rows[0]["SupplierId"].ToString() != "")
                {
                    ent.SupplierId = int.Parse(table.Rows[0]["SupplierId"].ToString());
                }
                if (table.Rows[0]["PurType"].ToString() != "")
                {
                    ent.PurType = int.Parse(table.Rows[0]["PurType"].ToString());
                }
                if (table.Rows[0]["AccountId"].ToString() != "")
                {
                    ent.AccountId = int.Parse(table.Rows[0]["AccountId"].ToString());
                }
                if (table.Rows[0]["PayTime"].ToString() != "")
                {
                    ent.PayTime = DateTime.Parse(table.Rows[0]["PayTime"].ToString());
                }
                if (table.Rows[0]["CreateMan"].ToString() != "")
                {
                    ent.CreateMan = int.Parse(table.Rows[0]["CreateMan"].ToString());
                }
                if (table.Rows[0]["CreateTime"].ToString() != "")
                {
                    ent.CreateTime = DateTime.Parse(table.Rows[0]["CreateTime"].ToString());
                }
                if (table.Rows[0]["AllMoney"].ToString() != "")
                {
                    ent.AllMoney = decimal.Parse(table.Rows[0]["AllMoney"].ToString());
                }
                if (table.Rows[0]["IsInvoicing"].ToString() != "")
                {
                    ent.IsInvoicing = int.Parse(table.Rows[0]["IsInvoicing"].ToString());
                }
                ent.InvoicingNum = table.Rows[0]["InvoicingNum"].ToString();
                if (table.Rows[0]["CheckMan"].ToString() != "")
                {
                    ent.CheckMan = int.Parse(table.Rows[0]["CheckMan"].ToString());
                }
                if (table.Rows[0]["CheckTime"].ToString() != "")
                {
                    ent.CheckTime = DateTime.Parse(table.Rows[0]["CheckTime"].ToString());
                }
                if (table.Rows[0]["IsCheck"].ToString() != "")
                {
                    ent.IsCheck = int.Parse(table.Rows[0]["IsCheck"].ToString());
                }
                if (table.Rows[0]["IsClear"].ToString() != "")
                {
                    ent.IsClear = int.Parse(table.Rows[0]["IsClear"].ToString());
                }
                if (table.Rows[0]["IsBlackOut"].ToString() != "")
                {
                    ent.IsBlackOut = int.Parse(table.Rows[0]["IsBlackOut"].ToString());
                }
                if (table.Rows[0]["BlackManId"].ToString() != "")
                {
                    ent.BlackManId = int.Parse(table.Rows[0]["BlackManId"].ToString());
                }
                ent.BlackManName = table.Rows[0]["BlackManName"].ToString();
                if (table.Rows[0]["BlackTime"].ToString() != "")
                {
                    ent.BlackTime = DateTime.Parse(table.Rows[0]["BlackTime"].ToString());
                }
                ent.Remark = table.Rows[0]["Remark"].ToString();
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_ProcurementPayment ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "PurchaseId");
        }

        public void Update(ProcurementPaymentEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_ProcurementPayment set ");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("PuDocuNum='" + model.PuDocuNum + "',");
            builder.Append("SupplierId=" + model.SupplierId + ",");
            builder.Append("PurType=" + model.PurType + ",");
            builder.Append("AccountId=" + model.AccountId + ",");
            builder.Append("PayTime='" + model.PayTime + "',");
            builder.Append("CreateMan=" + model.CreateMan + ",");
            builder.Append("CreateTime='" + model.CreateTime + "',");
            builder.Append("AllMoney=" + model.AllMoney + ",");
            builder.Append("IsInvoicing=" + model.IsInvoicing + ",");
            builder.Append("InvoicingNum='" + model.InvoicingNum + "',");
            builder.Append("CheckMan=" + model.CheckMan + ",");
            builder.Append("CheckTime='" + model.CheckTime + "',");
            builder.Append("IsCheck=" + model.IsCheck + ",");
            builder.Append("IsClear=" + model.IsClear + ",");
            builder.Append("Remark='" + model.Remark + "'");
            builder.Append(" where PurchaseId=" + model.PurchaseId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

